import { _decorator, Button, Label,Node, Sprite } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { AbNames } from '../../Config/ResConfig';
import { DW_Cfg, DW_MzCfg, DW_WtCfg, DW_ZqCfg, DWTYPE } from '../../Config/GameCfg';
import { GamePP_Ctr } from './GamePP_Ctr';
import { IThemeMatchItem, ThemeMatchResInfo } from '../../../AssetPackage/Props/Hall/Game/GameZT';
const { ccclass, property } = _decorator;

@ccclass('GameStart1Item_Ctr')
export class GameStart1Item_Ctr extends UIComponent {
    labType: Label;
    labPeople: Label;
    labDwtype: Label;
    labDwLv: Label;
    labXz: Label;
    sp: Node;
    type: number; //当前主题赛类型DWTYPE

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        this.sp = this.ViewNode('sp');
        this.labType = this.ViewComponent('labType',Label);
        this.labPeople = this.ViewComponent('labPeople',Label);
        this.labDwtype = this.ViewComponent('labDwtype',Label);
        this.labDwLv = this.ViewComponent('labDwLv',Label);
        this.labXz = this.ViewComponent('labXz',Label);

        this.AddButtonListener('btn',this,this.OnBtnStartClick);
    }

    private InitData(){
   
    }

    private registerEvent(){
        // globalThis.gameMgr._eventTarget.on(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private unRegisterEvent(){
        // globalThis.gameMgr._eventTarget.off(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private OnBtnStartClick(btn: Button){
        let ts_node = UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameStart1/GamePP',UIType.Full) as GamePP_Ctr;
        ts_node.dwType = this.type;
    }

    private setItemData(node: Node,data:ThemeMatchResInfo, idx:number){
        let lab = node.getChildByName('lab1').getComponent(Label);
        let labName = node.getChildByName('labName').getComponent(Label);
        let labLv = node.getChildByName('labLv').getComponent(Label);
        let labBei = node.getChildByName('labBei').getComponent(Label);
        let sphead = node.getChildByPath('head/headSpr').getComponent(Sprite);
        this.setHeadMat(sphead,1);
        
        lab.string = (idx+1).toString();
        if(this.hasProInObj(data,'name')){
            labName.string = data.name;
        }
        if(this.hasProInObj(data,'dwLv')){
            labLv.string = `Lv${data.dwLv}`;
        }
        let dwXzNum = 0;
        if(this.hasProInObj(data,'dwXzNum')){
            dwXzNum = data.dwXzNum;
        }
        labBei.string = `x${dwXzNum}`;
        if(this.hasProInObj(data,'avator')){
            this.LoadRemoteImage(data.avator,sphead);
        }
    }

    public setData(data:IThemeMatchItem){
        // console.log('======GameStart1Item_Ctr setData======',data);

        if(this.hasProInObj(data,'type')){
            this.type = data.type;
            this.labType.string = DW_Cfg[data.type];
            if(data.type === DWTYPE.DW_WENTI){
                if(data.dwType){
                    this.labDwtype.string = DW_WtCfg[data.dwType];
                }
            }else if(data.type === DWTYPE.DW_ZUQIU){
                if(data.dwType){
                    this.labDwtype.string = DW_ZqCfg[data.dwType];
                }
            }else if(data.type === DWTYPE.DW_MINGZU){
                if(data.dwType){
                    this.labDwtype.string = DW_MzCfg[data.dwType];
                }
            }
        }
        if(this.hasProInObj(data,'people')){
            this.labPeople.string = data.people.toString();
        }
        if(this.hasProInObj(data,'dwLv')){
            this.labDwLv.string = `Lv${data.dwLv}`;
        }
        let dwXzNum = data.dwXzNum || 0;
        if(this.hasProInObj(data,'dwXzNum')){
            dwXzNum = data.dwXzNum;
        }
        this.labXz.string = dwXzNum.toString();

        if(this.hasProInObj(data,'list')){
            for (let i = 0; i < 3; i++) {
                let node = this.sp.getChildByName(`temp${i+1}`);
                node.active = false;
            }
            for (let i = 0; i < data.list.length; i++) {
                let node = this.sp.getChildByName(`temp${i+1}`);
                node.active = true;
                this.setItemData(node, data.list[i],i);
            }
        }
    }
}


